import * as types from '../constants/ActionTypes';
import Request from '../utils/request';

const fetchDealerInfo = cb => (dispatch) => {
  Request.get({
    url: '/dealer/info',
    success: (data) => {
      dispatch(receiveDealerInfo(data));
      if (cb) {
        cb();
      }
    },
    fail: () => {
      dispatch(receiveDealerInfo({}));
      if (cb) {
        cb({
          err: '失败',
        });
      }
    },
    error: () => {
      dispatch(receiveDealerInfo({}));
      if (cb) {
        cb({
          err: 'error',
        });
      }
    },
  });
};

const receiveDealerInfo = dealerInfo => ({
  type: types.GET_DEALER_INFO,
  dealerInfo,
});

export default fetchDealerInfo;
